<template>
  <div class="page-resume" v-loading="aloading">
    <div class="page-resume-item flex-row align-items-center">
      <div class="page-resume-label">选择附件:</div>
      <div class="page-resume-content flex-row  align-items-center">
        <el-upload
            ref="uploadRef"
            class="upload-demo"
            action="https://api.bytesbay.cn/bole/attachment/uploadAsync"
            :show-file-list="false"
            :auto-upload="true"
            name="filename"
             accept=".jpg, .jpeg, .png, .pdf"
            :headers="updataParam"
            :on-success="onSuccess"
            :on-progress="onProgress"
            :on-error="onError"
            :before-upload="beforeupload"
          >
            <template #trigger v-if="dataList.length < 5">
              <pk-button :icon="Upload" :loading="loading" type="text" :disabled="dataList.length >= 5">上传</pk-button>
              <!-- <div class="flex-row align-items-center f-12 color-blur" v-if="dataList.length < 5">上传 <el-icon><Upload /></el-icon></div>  -->
            </template>
          </el-upload> 
          <div class="f-12 ml-12">仅支持PDF、PNG、JPG文件格式</div>
      </div>
    </div>
    <div class="page-resume-item mt-12">
      <div class="page-resume-content w-260">
        <div class="page-resume-radio mt-12" v-for="(item, index) in dataList" :key="index" @click="handlerClick(item)">
          <div class="page-resume-radiolabel" :class="{'page-resume-radiolabela': item.ative}">
            <div class="page-resume-radiolabelinfo" :class="{'hidden': !item.ative}"></div>
          </div>
          <div class="page-resume-radiocontent flex-row flex-1">
            <div class="flex-1">
              <div class="page-resume-radiotitle">{{ item.filename }}.{{ item.extension }}</div>
              <div class="page-resume-radiotime mt-8 gray-color">{{ item.time }} 上传</div>
            </div>
            <!-- <div class="page-resume-radioclose" @click="handerDel(item)">删除</div>  -->
          </div> 
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, defineEmits } from 'vue'
import { attachmentGetList, attachmentDelete } from '@/api/api'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Upload } from '@element-plus/icons-vue'
import { useSideStore } from '@/store/state'
import { getTimeYearStr } from '@/utils/utils'
import pkButton from '@/components/pkui/pkbutton/pkbutton.vue'
defineProps({
  aloading:{
    type: Boolean,
    default: false
  }
})
const emits = defineEmits(['onEmit'])
const counterStore = useSideStore()
const radio = ref(0)
const dataList = ref([])
const updataParam = reactive({
    Token: counterStore.token
  })

const loading = ref(false)
const getInit = () => {
  loading.value = false
  attachmentGetList().then(res => {
    dataList.value = res.data.map(item => {
      return {
        ...item,
        ative: false,
        time: getTimeYearStr(item.created_at, 'time')
      }
    })
  })
}

const handerDel = (data) => {
  ElMessageBox.confirm(
    '确定要删除?',
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      attachmentDelete({
        attachment_id: data.attachment_id
      }).then(res => {
        dataList.value = []
        getInit()
      })
    })
}

const handlerClick = (e) => {
  dataList.value = dataList.value.map(item => {
    return {
      ...item,
      ative: item.attachment_id == e.attachment_id
    }
  })
  emits('onEmit', e)
}

const onProgress = () => {
  loading.value = true
}

const onError = (e) => {
  console.log("-----------------", e)
  loading.value = false
}
const onSuccess = (e) => { 
  if (e.code != 1000) {
    if (e.is_alert) {
			ElMessage(e.msg)
		} 
    loading.value = false
    return
  }
  getInit()
}



onMounted(() => {
  getInit()
})
</script>

<style lang="scss" scoped>
.page-resume{
  &-item{
    display: flex;
    flex-direction: row;
    align-items: center;
  }
  &-label{
    color: #333;
    margin-right: 12px;
  }

  &-radio{
    display: flex;
    flex-direction: row;
    border-bottom: #ddd 1px solid;
    padding-bottom: 12px;
  }

  &-radio:last-child{
    border-bottom: none;
  }

  &-content{
    flex: 1
  }

 
  &-radiolabel{
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background-color: #fff;
    border: #ccc 1px solid;
    margin-right: 12px;
    position: relative;
  }

  &-radiolabelinfo{
    position: absolute;
    top: 50%;
    left: 50%;
    width: 8px;
    height: 8px;
    background: #06c;
    border-radius: 8px;
    transform: translateY(-50%) translateX(-50%);
  }

  &-radiolabela{
    border:#06c 1px solid; 
  }

  &-radiotitle{
    height: 16px;
    line-height: 16px;
    color: #333;
    font-size: 12px;
  }
  &-radiotime {
    color: #999;
    font-size: 12px;
  }
  &-radioclose{
    font-size: 12px;
    color: #cd2424;
  }
}
</style>